package 力扣.双指针;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class 删除链表的倒数第N个节点19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
         int i = 0;
         ListNode dummy = new ListNode();
         dummy.next = head;
         ListNode fast = dummy;
         ListNode slow = dummy;
         while (fast != null && i <= n){
             fast = fast.next;
             i++;
         }
         if (fast != null){
             while (fast != null){
                 fast = fast.next;
                 slow = slow.next;
             }
             slow.next = slow.next.next;
         }else {
                 dummy.next = dummy.next.next;
                 return dummy.next;
         }
         return dummy.next;
    }
}
